<?php

namespace Admin\Controller;

use Common\Controller\CommonController;

class LayoutController extends CommonController
{
    public function menu()
    {
        $menu = M("node");
        $id = $_POST["id"];
        if ($id == null || "" . equals($id)) {
            //$list=$menu->select();
            $con['level'] = array('elt', 2);
            $con['ismenu'] = 1;
            $list = $menu->where($con)->select();
            //$list=$list["0"];attributes
            //$list['info'] = 'info';
            //dump($list);
            $list = json_encode($list);
            //$list.put('state','open');
            echo $list; //输出json数据
        } else {
            $list = $menu->where('pid=' + $id)->select();
        }
    }

    // 后台首页 查看系统信息
    public function main()
    {
        $info = array(
            '操作系统'                 => PHP_OS,
            '运行环境'                 => $_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式'              => php_sapi_name(),
            'PHP版本'                => phpversion(),
            //'ThinkPHP版本'=>THINK_VERSION.' [ <a href="http://thinkphp.cn" target="_blank">查看最新版本</a> ]',
            '上传附件限制'               => ini_get('upload_max_filesize'),
            '执行时间限制'               => ini_get('max_execution_time') . '秒',
            '服务器时间'                => date("Y年n月j日 H:i:s"),
            '北京时间'                 => gmdate("Y年n月j日 H:i:s", time() + 8 * 3600),
            '服务器域名/IP'             => $_SERVER['SERVER_NAME'] . ' [ ' . gethostbyname($_SERVER['SERVER_NAME']) . ' ]',
            '剩余空间'                 => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
            'register_globals'     => get_cfg_var("register_globals") == "1" ? "ON" : "OFF",
            'magic_quotes_gpc'     => (1 === get_magic_quotes_gpc()) ? 'YES' : 'NO',
            'magic_quotes_runtime' => (1 === get_magic_quotes_runtime()) ? 'YES' : 'NO',
        );
        $this->assign('info1', $info);
        // dump($info);
        $this->display();
    }

    //注册用户提交处理
    public function add()
    {
        $m = D("user");
        if (!$m->create($_POST)) {
            $this->error($m->getError(), 3);
        } else {
            if ($result = $m->add($_POST)) {
                $this->success('添加成功！', 1);
            } else {
                $this->error('添加失败！', 0);
            }
        }
    }

    //显示首页公告
    public function gonggao()
    {
        $n = D("Gonggao");
        $list = $n->relation(true)->order("time desc")->select();
        //$list = $n->relation(true)->where($condition)->page($page,$rows)->order('id desc')->select();
        //dump($n->getLastSql());
        //$list = $n->select();
        //        dump($list);
        $this->assign('list', $list[0]);
        // dump($info);
        $this->display();
    }

    public function head()
    {
        $menulist = $this->getMenulist();
        $this->assign("menulist", $menulist);
        // dump($menulist);
        $usercon['update_time'] = array("egt", date("Y-m-d H:i:s", time() - (10 * 60)));
        $user = M('User');
        $online = $user->where($usercon)->count();
        $this->assign("online", $online);

        // $url='http://api.cnsms.cn/?ac=gc&uid=104245&pwd=e91ad915ba65b8de9fd53cafeb9fdd95';
        // $duanxincount=getHtml($url);
        // $duanxincount=explode('||', $duanxincount);
        // $duanxincount=$duanxincount[1];
        $duanxincount = 0;
        $this->assign("duanxincount", $duanxincount);
        $this->assign("dbname", C('DB_NAME'));
        //$this->display();
    }

    public function center()
    {
        $this->display();
    }

    public function portal()
    {
        $sushelist = D('sushe')->relation(true)->select();
        $this->assign('sushelist', $sushelist);

        $emp_zaizhi = M('Employee')->where(array('lizhistatus' => '在职'))->count();
        $emp_lizhi = M('Employee')->where(array('lizhistatus' => '离职'))->count();
        $emp_zhuanzheng = M('Employee')->where(array('lizhistatus' => '在职', 'isregular' => '是'))->count();
        $emp_shiyong = M('Employee')->where(array('lizhistatus' => '在职', 'isregular' => '否'))->count();
        $this->assign('emp_zaizhi', $emp_zaizhi);
        $this->assign('emp_lizhi', $emp_lizhi);
        $this->assign('emp_zhuanzheng', $emp_zhuanzheng);
        $this->assign('emp_shiyong', $emp_shiyong);

        ////////////////////////////////////////获报餐人数
        $con_jinrizao['zaoshang'] = '吃';
        $con_jinrizao['addtime'] = array('like', date('Y-m-d', time()) . '%');
        $userInfoRes['jinrizaototal'] = M('baocan')->where($con_jinrizao)->count();

        $con_jinrishang['shangwu'] = '吃';
        $con_jinrishang['addtime'] = array('like', date('Y-m-d', time()) . '%');
        $userInfoRes['jinrishangtotal'] = M('baocan')->where($con_jinrishang)->count();

        $con_jinrixia['xiawu'] = '吃';
        $con_jinrixia['addtime'] = array('like', date('Y-m-d', time()) . '%');
        $userInfoRes['jinrixiatotal'] = M('baocan')->where($con_jinrixia)->count();

        $con_mingrizao['zaoshang'] = '吃';
        $con_mingrizao['addtime'] = array('like', date('Y-m-d', time() + 86400) . '%');
        $userInfoRes['mingrizaototal'] = M('baocan')->where($con_mingrizao)->count();

        $con_mingrishang['shangwu'] = '吃';
        $con_mingrishang['addtime'] = array('like', date('Y-m-d', time() + 86400) . '%');
        $userInfoRes['mingrishangtotal'] = M('baocan')->where($con_mingrishang)->count();

        $con_mingrixia['xiawu'] = '吃';
        $con_mingrixia['addtime'] = array('like', date('Y-m-d', time() + 86400) . '%');
        $userInfoRes['mingrixiatotal'] = M('baocan')->where($con_mingrixia)->count();

        $this->assign('baocan', $userInfoRes);
        ////////////////////////////////////////获报餐人数
        ////////////////////////////////////////////岗位人员分布
        $gangweilist = M('gangwei')->order('gname asc')->select();
        //dump($gangweilist);
        $this->assign('gangweilist', $gangweilist);
        ////////////////////////////////////////////资产分布
        $this->assign('deptzichanlist', $this->deptzichan());
        $this->assign('detpgangwei', $this->detpgangwei());
        $this->display();
    }

    public function detpgangwei()
    {
        $n = D("Dept");
        $gangweilist = M('gangwei')->order('gname asc')->select();
        $list = $n->order('title desc')->select();
        foreach ($list as $key => $value) {
            foreach ($gangweilist as $k => $v) {
                $list[$key][$v['id']] = $this->getEmployeeNum($value['id'], $v['id']);
            }
        }
        //dump($list);
        $list = json_encode($list);
        return ($list); //输出json数据
    }

    private function getEmployeeNum($deptid, $gangweiid)
    {
        $con['deptid'] = $deptid;
        $con['gangweiid'] = $gangweiid;
        $con['lizhistatus'] = '在职';
        $num = M('Employee')->cache(60 * 30)->where($con)->count();
        return $num;
    }

    public function deptzichan()
    {
        $n = D("Dept");
        $list = $n->order('title desc')->select();
        foreach ($list as $key => $value) {
            $list[$key]['shouji'] = M('Zctel')->cache(600)->where(array('deptid' => $value['id']))->count();
            $list[$key]['shoujika'] = M('Zctelcard')->cache(600)->where(array('deptid' => $value['id']))->count();
            $list[$key]['zczhanghao'] = M('zczhanghao')->cache(600)->where(array('deptid' => $value['id']))->count();
            $list[$key]['zcjingjianumber'] = M('zcjingjianumber')->cache(600)->where(array('deptid' => $value['id']))->count();
            $list[$key]['zcfuwuqi'] = M('zcfuwuqi')->cache(600)->where(array('deptid' => $value['id']))->count();
            $list[$key]['zcdomain'] = M('zcdomain')->cache(600)->where(array('deptid' => $value['id']))->count();
        }
        //dump($list);
        $list = json_encode($list);
        return ($list); //输出json数据
    }

    public function west()
    {
        import('ORG.Util.Auth'); //加载类库
        $auth = new Auth();
        if ($_SESSION['user']['account'] == admin) {
            $authlist = M('node')->order('sort asc')->select();
        } else {
            $authlist = $auth->getAuthList($_SESSION['user']['id']);
        }
        $menulist = array();
        $menulist = $this->erwei($authlist);
        //        dump($authlist);
        $this->assign("menulist", $menulist);
        $this->display();
    }

    private function getMenulist()
    {
        $auth = new \Think\Auth();
        if (session('user.account') == admin) {
            $authlist = M('node')->order('sort asc')->select();
        } else {
            $authlist = $auth->getAuthList(session('user.id'));
        }
        $menulist = array();
        $menulist = $this->erwei($authlist);
        return $menulist;
    }

    //一维数组无限级分类
    public function erwei($cate, $pid = 0)
    {
        $arr = array();
        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                if ($v['ismenu'] == 1) {
                    $v['child'] = self::erwei($cate, $v['id']);
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }
}
